//
//  HYLRegisterView.swift
//  iOSwechat
//
//  Created by apple on 16/2/27.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

// MARK: - 按钮的点击协议
protocol HYLRegisterViewDelegate: NSObjectProtocol{
    // 注册
    func registerButtonClick()
    // 登录
    func loginButtonClick()
}

class HYLRegisterView: UIView {
    
    // 代理属性
    weak var registerViewDelegate : HYLRegisterViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.whiteColor()
        
        showUI()
    }
    
    // 从xib/storyboard里面加载会走这个方法
    // 当子类实现init(frame: CGRect)就不会继承父类的构造函数init?(coder aDecoder: NSCoder),就不能从xib/storyboard里面加载
    // 只有同时实现init(frame: CGRect)和init?(coder aDecoder: NSCoder)才能保证view即可以使用代码创建,也可以使用xib/storyboar来创建
    required init?(coder aDecoder: NSCoder) {
        //        fatalError("init(coder:) has not been implemented")
        // 做自己的事情
        super.init(coder: aDecoder)
    }
    
    
    // 加载小房子
    lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 转轮
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮罩
    lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 文字
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        label.text = "关注一些人,看看有上面惊喜看看有上面惊喜"
        label.textColor = UIColor.darkTextColor()
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(18)
        return label
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
        let registerButton = UIButton()
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        registerButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 给注册添加点击事件
        registerButton.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerButton
    }()
    
    // 登录按钮
    lazy var loginButton: UIButton = {
        let loginButton = UIButton()
        loginButton.setTitle("登录", forState: UIControlState.Normal)
        loginButton.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        // 给登录按钮添加点击事件
        loginButton.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginButton
    }()
    
    // MARK: - 设置界面上的信息
    func setupRegisterInfo(imageName:String , message:String){
        messageLabel.text = message
        messageLabel.sizeToFit()
        homeView.image = UIImage(named: imageName)
        iconView.hidden = true
        
    }
    
    
    // MARK: - 界面上的布局 
    // command + option + 箭头   实现缩放
    func showUI() {
        
        // 设置背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        //1. 将懒加载添加到父控制器中
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //2. 设置约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        

        // 滚轮的X
        addConstraint(NSLayoutConstraint(item : iconView, attribute : NSLayoutAttribute.CenterX, relatedBy : NSLayoutRelation.Equal, toItem : self, attribute : NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 滚轮的Y
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))

 
        
        
        // 遮罩 centerX
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -120))
        
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
        // 房子的X
        addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 房子的Y
        addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // messageLabel W
        // 宽度 240
        // 注意: 当toItem为nil时,attribute必须填:NSLayoutAttribute.NotAnAttribute
        addConstraint( NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // messageLabel X
        addConstraint( NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 房子的Top
        addConstraint( NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        
        // 注册按钮 Top
        addConstraint( NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 注册按钮 Top
        addConstraint( NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        
        // 注册按钮 Width
        addConstraint( NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 100))
        
        // 注册按钮 Height
        addConstraint( NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 35))
        
        // 登录按钮 Top
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        // 登录按钮 Right
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        // 登录按钮 Width
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
        // 登录按钮 Height
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
    }
    
    
    // 图片转动
    func startRotationAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        //// 要加上这句,不然切换到其他tabBar或者退出到桌面在切回来动画就停止了
        animation.removedOnCompletion = false
        
        // 给图片点击动画
        iconView.layer.addAnimation(animation, forKey: nil)
    }
    
    // 注册方法
    func registerButtonClick(){
        registerViewDelegate?.registerButtonClick()
    }
    
    // 登录方法
    func loginButtonClick() {
        registerViewDelegate?.loginButtonClick()
    }
    
    
    
}
